/*
 
    Copyright (c) 2010 Jeffrey Dik <s450r1@gmail.com>
    Copyright (c) 2010 Martin Sustrik <sustrik@250bpm.com>
    Copyright (c) 2010 Ralf Westphal <info@ralfw.de>
     
    This file is derived from clrzmq and is part of NZMQ, nzmq.codeplex.com.
     
    clrzmq and NZMQ are free software; you can redistribute it and/or modify it under
    the terms of the Lesser GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
     
    clrzmq and NZMQ is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    Lesser GNU General Public License for more details.
     
    You should have received a copy of the Lesser GNU General Public License
    along with this program. If not, see <http://www.gnu.org/licenses/>.

*/
using System;
using System.Threading.Tasks;
using nzmq.causalities;
using nzmq.raw;

namespace nzmq
{
    public class NzmqContext : IDisposable
    {
        private IntPtr _handle;


        public NzmqContext() : this(1) {}
        public NzmqContext(int numberOfIOthreads)
        {
            _handle = libzmq.zmq_init(numberOfIOthreads);
            if (_handle == IntPtr.Zero) throw new NzmqException();
        }

        ~NzmqContext()
        {
            Dispose(false);
        }


        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (_handle != IntPtr.Zero)
            {
                var rc = libzmq.zmq_term(_handle);
                _handle = IntPtr.Zero;
                if (rc != 0) throw new NzmqException();
            }
        }


        public NzmqServiceSocket CreateServiceSocket(string addressToBindTo = "")
        {
            var sck = CreateRawSocket(NzmqSocketTypes.Reply);
            if (!string.IsNullOrEmpty(addressToBindTo)) sck.Bind(addressToBindTo);
            return new NzmqServiceSocket(sck);
        }

        public NzmqClientSocket CreateClientSocket(string serviceAddress = "")
        {
            var sck = CreateRawSocket(NzmqSocketTypes.Request);
            if (!string.IsNullOrEmpty(serviceAddress)) sck.Connect(serviceAddress);
            return new NzmqClientSocket(sck);
        }


        public NzmqClientSocket CreatePairSocket()
        {
            var sck = CreateRawSocket(NzmqSocketTypes.Pair);
            return new NzmqClientSocket(sck);
        }


        public Task HostService(Action<NzmqServiceSocket> service, string addressToBindTo)
        {
            return Task.Factory.StartNew(() => service(this.CreateServiceSocket(addressToBindTo)));
        }


        public NzmqPublisherSocket CreatePublisherSocket(string addressToBindTo = "")
        {
            var sck = CreateRawSocket(NzmqSocketTypes.Pub);
            if (!string.IsNullOrEmpty(addressToBindTo)) sck.Bind(addressToBindTo);
            return new NzmqPublisherSocket(sck);
        }

        public NzmqSubscriberSocket CreateSubscriberSocket(string publisherAddress = "")
        {
            var sck = CreateRawSocket(NzmqSocketTypes.Sub);
            if (!string.IsNullOrEmpty(publisherAddress)) sck.Connect(publisherAddress);
            return new NzmqSubscriberSocket(sck);
        }


        public NzmqUpstreamSocket CreateUpstreamSocket(string addressToBindTo = "")
        {
            var sck = CreateRawSocket(NzmqSocketTypes.Push);
            if (!string.IsNullOrEmpty(addressToBindTo)) sck.Bind(addressToBindTo);
            return new NzmqUpstreamSocket(sck);
        }

        public NzmqDownstreamSocket CreateDownstreamSocket(string pusherAddress = "")
        {
            var sck = CreateRawSocket(NzmqSocketTypes.Pull);
            if (!string.IsNullOrEmpty(pusherAddress)) sck.Connect(pusherAddress);
            return new NzmqDownstreamSocket(sck);
        }


        
        public LibzmqSocket CreateRawSocket(NzmqSocketTypes socketType)
        {
            if (_handle == IntPtr.Zero) throw new NzmqException();

            var socketHandle = libzmq.zmq_socket(_handle, (int)socketType);
            return new LibzmqSocket(socketHandle);
        }


        #region Experimental
        public void Try(Action action)
        {
            throw new NotImplementedException();
        }

        public NzmqCausality Catch(string exceptionHandlerAddress, Action<Exception> exceptionHandler)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}